$(function () {
    var url = "/oa/t-role";
    var token = sessionStorage.getItem('token');
    var oprateFormatter = function (value, row, index) {
        // if (row.roleName != "管理员" && row.roleName != "教师" && row.roleName != "学生") {
        //     return [
        //         '<button id="tb_perm" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">权限</button>',
        //         '<button id="tb_edit" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">编辑</button>',
        //         '<button id="tb_delete" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">删除</button>',
        //     ].join('');
        // }else {
        //     return [
        //         '<button id="tb_perm" disabled="disabled" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">权限</button>',
        //         '<button id="tb_edit" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px" data-toggle="modal">编辑</button>',
        //         '<button id="tb_delete" disabled="disabled" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">删除</button>',
        //     ].join('');
        // }

        return [
            '<button id="tb_perm" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">权限</button>',
            '<button id="tb_edit" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">编辑</button>',
            '<button id="tb_delete" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">删除</button>',
        ].join('');
    };

    var operateEvents = function(context) {
        var me = context;
        return {
            "click #tb_perm" : function(e, value, row, index) {
                me.perm([row]);
            },

            "click #tb_edit" : function(e, value, row, index) {
                me.edit([row]);
            },

            "click #tb_delete" : function(e, value, row, index) {
                me.remove([row]);
            },
        };
    };

    var page = $.fn.cookePageTable({
        URL: url,
        dialog: {
            domid: '#editModel',
            title: '用户',
            validate : {
                userName: {
                    required: true,
                },
                userEmail: {
                    required: true,
                    minlength: 4,
                    maxlength: 128,
                    email:true,
                },
                userPhone: {
                    required: true,
                    minlength: 3,
                    maxlength: 19,
                    digits:true,
                },
            },

            fieldLoader: {
                //roleId : listRole,
            },
        },

        toolbar: {
            add: '#addBtn',
        },

        table: {
            domid: "#table",
            uniqueId: "roleId", //每一行的唯一标识，一般为主键列
            uniqdesc: function(row) {
                return row.roleName;
            },                  //每一行的唯一标识描述字段信息
            columns: [ {
                field: 'roleName',
                title: '角色名称',
                align: 'center'
            }, {
                field: 'createTime',
                title: '创建时间',
                align: 'center'
            }, {
                field: 'updateTime',
                title: '修改时间',
                align: 'center'
            }, {
                field: 'operate',
                title: '操作',
                align: 'center',
                events: operateEvents,
                formatter: oprateFormatter //自定义方法，添加操作按钮
            }],
        },
    });

    $('#tableLeft').bootstrapTable({
        url: '/oa/t-menu/powerByRole?token='+token,
        type: 'GET',
        height: 202,
        columns: [{
            checkbox: true,
            visible: true
        }, {
            field: 'menuName',
            title: '未赋予权限',
            align: 'center'
        }]
    });

    $('#tableRight').bootstrapTable({
        url: '/oa/t-menu/powerByRole?token='+token,
        type: 'GET',
        height: 202,
        columns: [{
            checkbox: true,
            visible: true
        }, {
            field: 'menuName',
            title: '已赋予权限',
            align: 'center'
        }]
    });

    page.perm = function (arrselections) {
        var row = arrselections[0];
        $('#powerModel').modal('show');
        $('#tableLeft').bootstrapTable('refresh', {
            query : {
                status: 0,
                roleId: row.roleId,
            }
        });

        $('#tableRight').bootstrapTable('refresh', {
            query : {
                status: 1,
                roleId: row.roleId,
            }
        });

        $("#addPower").click(function () {
            var str = "";
            var row1 = $('#tableLeft').bootstrapTable('getSelections');
            for (var i = 0; i < row1.length; i++) {
                str += row1[i].menuId + ','
            }
            var param = {
                roleId: row.roleId,
                menuId: str,
                status: 1
            };
            $.ajax({
                url: '/oa/t-power/setPower',
                type: "POST",
                data: param,
                dataType: "json",
                headers: {
                    token: "" + token
                },
                success: function (data) {
                    if (data.code == 200) {
                        $.message(data.msg);
                    } else if (data.code == 401) {
                        window.location.href = 'login.html'
                    } else {
                        $.message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                    $('#tableLeft').bootstrapTable('refresh', {
                        query : {
                            status: 0,
                            roleId: row.roleId,
                        }
                    });
                    $('#tableRight').bootstrapTable('refresh', {
                        query: {
                            status: 1,
                            roleId: row.roleId,
                        }
                    });
                }
            });
        });
        $("#removePower").click(function () {
            var str = "";
            var row1 = $('#tableRight').bootstrapTable('getSelections');
            for (var i = 0; i < row1.length; i++) {
                str += row1[i].menuId + ','
            }
            var param = {
                roleId: row.roleId,
                menuId: str,
                status: 0
            };

            $.ajax({
                url: '/oa/t-power/setPower',
                type: "POST",
                data: param,
                dataType: "json",
                headers: {
                    token: "" + token
                },
                success: function (data) {
                    if (data.code == 200) {
                        $.message(data.msg);
                    } else if (data.code == 401) {
                        window.location.href = 'login.html'
                    } else {
                        $.message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                    $('#tableLeft').bootstrapTable('refresh', {
                        query: {
                            status: 0,
                            roleId: row.roleId,
                        }
                    });
                    $('#tableRight').bootstrapTable('refresh', {
                        query: {
                            status: 1,
                            roleId: row.roleId,
                        }
                    });
                }
            });
        });
    };
});
